package com.zzc.legou.base.upload.controller;

import com.zzc.legou.base.upload.utils.FileDfsUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.annotation.Resources;

@RestController
public class FileController {

    @Resource
    private FileDfsUtil fileDfsUtil;


    /**
     * 上传照片
     */
    @ApiOperation(value = "上传文件信息", notes = "测试FastDFS文件上传")
    @PostMapping(value = "/uploadFile", headers = "Content-Type=multipart/form-data")
    public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) {
        String result = null;
        try {
            String upload = fileDfsUtil.upload(file);
            if (!StringUtils.isEmpty(upload)) {
                result = upload;
            } else {
                result = "上传失败";
            }
        } catch (Exception e) {

            result = "服务器异常";
        }
        System.out.println("result = " + result);
        return ResponseEntity.ok(result);
    }

    /**
     * 删除照片
     */
    @ApiOperation(value = "删除照片", notes = "测试照片删除按钮")
    @GetMapping(value = "/deleteByPath")
    public ResponseEntity<String> deleteByPath(String filePathName) {
        fileDfsUtil.deleteFile(filePathName);
        return ResponseEntity.ok("success");
    }


}
